package com.rapiddevsample;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.v.rapiddev.adpater.FFViewHolder;
import com.v.rapiddev.adpater.FastAdapter;
import com.v.rapiddev.base.AppManager;
import com.v.rapiddev.preferences.AppPreferences;
import com.v.rapiddev.pulltorefresh.PullToRefreshBase;
import com.v.rapiddev.pulltorefresh.PullToRefreshScrollView;
import com.v.rapiddev.utils.V;
import com.v.rapiddev.views.MyGirdView;
import com.v.rapiddev.views.MyListView;

/**
 * ClassType:
 * User:wenyunzhao
 * ProjectName:RapidDevSample
 * Package_Name:com.rapiddevsample
 * Created on 2016-11-24 14:23
 */

public class PullToRefreshSc extends BaseActivity {
    private GridView gv;
    private MyListView lv;
    private PullToRefreshScrollView sv;
    private FastAdapter<String> adapter_lv, adapter_gv;
    private Activity context;


    @Override
    public void init() {
        setContentView(R.layout.layout_pull);
        gv = (GridView) findViewById(R.id.gv);
        lv = (MyListView) findViewById(R.id.lv);
        sv = (PullToRefreshScrollView) findViewById(R.id.sv);
        context = this;
        //设置上下拉模式
        sv.setMode(PullToRefreshBase.Mode.BOTH);

        sv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                //上啦
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //模拟加载数据  dosomething
                        sv.onRefreshComplete();
                        Toast.makeText(context, "刷新成功", Toast.LENGTH_SHORT).show();
                    }
                }, 1000);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                //下拉
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //模拟加载更多  dosomething
                        sv.onRefreshComplete();
                        Toast.makeText(context, "加载成功", Toast.LENGTH_SHORT).show();
                    }
                }, 1000);
            }
        });
        initAd();

        addData();
        sv.getRefreshableView().smoothScrollTo(0, 0);

        new AppPreferences(this).put("a", 123);

        V.d("a:" + new AppPreferences(this).getInt("a", -1));
    }

    @Override
    public void resume() {

    }

    @Override
    public void destroy() {
        AppManager.getAppManager().finishActivity(this);
    }

    private void addData() {
        adapter_gv.addItem("http://img3.imgtn.bdimg.com/it/u=3277863786,1296043081&fm=21&gp=0.jpg");
        adapter_gv.addItem("http://img3.imgtn.bdimg.com/it/u=3277863786,1296043081&fm=21&gp=0.jpg");
        adapter_gv.addItem("http://img3.imgtn.bdimg.com/it/u=3277863786,1296043081&fm=21&gp=0.jpg");
        adapter_gv.addItem("http://img3.imgtn.bdimg.com/it/u=3277863786,1296043081&fm=21&gp=0.jpg");
        adapter_gv.addItem("http://img3.imgtn.bdimg.com/it/u=3277863786,1296043081&fm=21&gp=0.jpg");
        adapter_gv.addItem("http://img3.imgtn.bdimg.com/it/u=3277863786,1296043081&fm=21&gp=0.jpg");
        adapter_gv.notifyDataSetChanged();


        for (int i = 0; i < 20; i++) {
            adapter_lv.addItem("");
        }
        adapter_lv.notifyDataSetChanged();
    }

    private void initAd() {
        adapter_lv = new FastAdapter<String>(context, R.layout.item_lv) {
            @Override
            public View getViewHolder(int position, View convertView, ViewGroup parent) {
                FFViewHolder viewHolder = FFViewHolder.get(context, convertView, parent, R.layout.item_lv, position);
                MyGirdView gridView = (MyGirdView) viewHolder.getView(R.id.ggv);
                FastAdapter<String> adapter_ggv = new FastAdapter<String>(context, R.layout.item_gv) {
                    @Override
                    public View getViewHolder(int position, View convertView, ViewGroup parent) {
                        FFViewHolder viewHolder = FFViewHolder.get(context, convertView, parent, R.layout.item_gv, position);
                        ImageView imageView = (ImageView) viewHolder.getView(R.id.imvv);
                        Glide.with(context).load(getItem(position)).placeholder(R.mipmap.ic_launcher).into(imageView);
                        return viewHolder.getConvertView();
                    }
                };
                gridView.setAdapter(adapter_ggv);

                for (int i = 0; i < 9; i++) {
                    adapter_ggv.addItem("http://img4.duitang.com/uploads/item/201509/28/20150928151745_XF2B5.jpeg");
                }
                adapter_ggv.notifyDataSetChanged();
                return viewHolder.getConvertView();
            }
        };
        lv.setAdapter(adapter_lv);

        adapter_gv = new FastAdapter<String>(context, R.layout.item_gv) {
            @Override
            public View getViewHolder(int position, View convertView, ViewGroup parent) {
                FFViewHolder viewHolder = FFViewHolder.get(context, convertView, parent, R.layout.item_gv, position);
                Glide.with(context).load(getItem(position)).placeholder(R.mipmap.ic_launcher).into((ImageView) viewHolder.getView(R.id.imvv));
                return viewHolder.getConvertView();
            }
        };

        gv.setAdapter(adapter_gv);
    }
}
